package cn.fl.ref.scmbaseinfo.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cmm.comm.facade.framework.api.ISysDictionaryApiService;
import cmm.comm.facade.framework.dto.SysDictionaryDTO;
import cmm.mid.core.framework.dto.DataResultDTO;
import cmm.mid.core.framework.page.KeyValueVO;
import cmm.mid.core.framework.page.Pager;
import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.BaseLogUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.framework.base.context.CurrentThreadContext;
import cn.fl.ref.crbaseinfo.transform.CrBaseInfoVOTransform;
import cn.fl.ref.crbaseinfo.vo.CrBaseInfoVO;
import cn.fl.ref.scmbaseinfo.service.IScmBaseInfoService;
import cn.fl.ref.scmbaseinfo.transform.ScmBaseInfoVOTransform;
import cn.fl.ref.scmbaseinfo.vo.ScmBaseInfoVO;
import cn.fl.ref.scmbaseinfo.vo.ScmChrgParaVO;
import fl.flow.facade.flow.dto.FlowStartParamsDTO;
import fl.ref.crbaseinfo.dto.CrBaseInfoDTO;
import fl.ref.scmbaseinfo.condition.ScmBaseInfoQueryCondition;
import fl.ref.scmbaseinfo.constant.ScmInfoConstant;
import fl.ref.scmbaseinfo.dto.ScmBscInfoDTO;
import fl.ref.scmbaseinfo.dto.ScmChrgParaDTO;
import fl.ref.scmbaseinfo.service.IScmBaseInfoApiService;
import net.sf.json.JSONArray;

@Service
public class ScmBaseInfoImpl implements IScmBaseInfoService {
	private static final Logger logger = LoggerFactory.getLogger(ScmBaseInfoImpl.class);
	
	@Autowired
	private IScmBaseInfoApiService scmBaseInfoApiService;
	/**
	 * 数据字典服务
	 */
	@Autowired
	private ISysDictionaryApiService sysDictionaryApiService;

	@Override
	public ScmBaseInfoVO selectById(Long id) throws BaseException {
		ScmBscInfoDTO dto = scmBaseInfoApiService.selectById(id);
		ScmBaseInfoVO vo = ScmBaseInfoVOTransform.toVO(dto);
		return vo;
	}

	@Override
	public List<ScmBaseInfoVO> selectBscLstByCondition(ScmBaseInfoQueryCondition condition) throws BaseException {
		List<ScmBscInfoDTO> lstDto = scmBaseInfoApiService.selectBscLstByCondition(condition);
		List<ScmBaseInfoVO> lstVo = ScmBaseInfoVOTransform.toVOList(lstDto);
		return lstVo;
	}

	@Override
	public ScmBaseInfoVO selectDetailById(Long id) throws BaseException {
		
		return null;
	}

	@Override
	public Long add(ScmBaseInfoVO entity) throws BaseException {
		return scmBaseInfoApiService.add(ScmBaseInfoVOTransform.toDTO(entity));
	}

	@Override
	public Long insertByBatch(List<ScmChrgParaVO> chrgLst) throws BaseException {
		List<ScmChrgParaDTO> lst = ScmBaseInfoVOTransform.toCharDTOList(chrgLst);
		JSONArray obj = JSONArray.fromObject(lst);
		logger.info("*****insertByBatch toCharDTOList******"+obj.toString());		
		return scmBaseInfoApiService.insertByBatch(ScmBaseInfoVOTransform.toCharDTOList(chrgLst));
	}

	@Override
	public List<ScmChrgParaVO> selectChrgDataByScmId(Long fncScmId) throws BaseException {
		List<ScmChrgParaDTO> lstDto = scmBaseInfoApiService.selectChrgDataByScmId(fncScmId);
		List<ScmChrgParaVO> lstVo = ScmBaseInfoVOTransform.toCharVOList(lstDto);
		//设置保证金到期处理方式
		for(ScmChrgParaVO paraVo:lstVo){
			if(paraVo.getMrgnUseMtd()!=null && !"".equals(paraVo.getMrgnUseMtd().toString()) && !"null".equals(paraVo.getMrgnUseMtd().toString())){
				List<KeyValueVO> mrgnUseMtdLst = sysDictionaryApiService.getKvList(ScmInfoConstant.FNC_SCM_CHRG_PARM_MRGN_USE_MTD, 
						CurrentThreadContext.getFlServiceSysCd(), CurrentThreadContext.getCurrentOrgCd());//保证金使用方式
				for(KeyValueVO kv:mrgnUseMtdLst){
					if(kv.getKey().equals(paraVo.getMrgnUseMtd().toString())){
						paraVo.setMrgnUseMtdDes(kv.getValue());
					}
				}
			}else{
				paraVo.setMrgnUseMtdDes("");
			}
		}//end  保证金处理方式
		return lstVo;
	}

	@Override
	public CrBaseInfoVO selectByCrId(String crId) throws BaseException {
		CrBaseInfoDTO dto = scmBaseInfoApiService.selectByCrId(crId);
		CrBaseInfoVO vo = CrBaseInfoVOTransform.toVO(dto);
		return vo;
	}

	@Override
	public boolean removeById(Long id) throws BaseException {
		
		return false;
	}

	@Override
	public boolean removeByIds(List<Long> ids) throws BaseException {
		
		return false;
	}

	@Override
	public int modify(ScmBaseInfoVO entity) throws BaseException {
		return scmBaseInfoApiService.updateScmBscInfo(ScmBaseInfoVOTransform.toDTO(entity));
	}

	@Override
	public Pager searchListPage(Pager page) throws BaseException {
		if (null == page)
			page = new Pager();
		return scmBaseInfoApiService.searchListPage(page);
	}

	@Override
	 public List<KeyValueVO> searchWthrKVList()throws BaseException {
		List<KeyValueVO> kvList = new ArrayList<>();
		KeyValueVO keyValueVO = new KeyValueVO();
		keyValueVO.setKey("1");
		keyValueVO.setValue("是");
		kvList.add(keyValueVO);
		
		KeyValueVO keyValueVO1 = new KeyValueVO();
		keyValueVO1.setKey("0");
		keyValueVO1.setValue("否");
		kvList.add(keyValueVO1);
		return kvList;
	}

	@Override
	public DataResultDTO addFlowStart(FlowStartParamsDTO flowStartParamsDTO) {
		if(StringUtils.isEmpty(flowStartParamsDTO.getCrtUsrId()) || StringUtils.isEmpty(flowStartParamsDTO.getBusinessKey())){
			BaseLogUtils.info(logger, "addFlowStart", "用户ID和业务数据主键必须要存在!",CurrentThreadContext.getCurrentUserName());
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_600, "用户ID和业务数据主键必须要存在!");
		}
		return scmBaseInfoApiService.addFlowStart(flowStartParamsDTO);
	}

	@Override
	public Long deleteChrgDataByScmId(Long fncScmId) throws BaseException {
		return scmBaseInfoApiService.deleteChrgDataByScmId(fncScmId);
	}

	@Override
	public Integer selectExistScmCntByCondition(ScmBaseInfoQueryCondition condition) {
		return scmBaseInfoApiService.selectExistScmCntByCondition(condition);
	}

	@Override
	public List<Map<String, Object>> selectBscLstMapByCondition(ScmBaseInfoQueryCondition condition)
			throws BaseException {
		return scmBaseInfoApiService.selectBscLstMapByCondition(condition);
	}

	@Override
	public List<KeyValueVO> searchWthrKVListBZJ() {
		List<KeyValueVO> kvList = new ArrayList<>();
		KeyValueVO keyValueVO = new KeyValueVO();
		keyValueVO.setKey("1");
		keyValueVO.setValue("是");
		kvList.add(keyValueVO);
		
		KeyValueVO keyValueVO1 = new KeyValueVO();
		keyValueVO1.setKey("0");
		keyValueVO1.setValue("否");
		kvList.add(keyValueVO1);
		
		KeyValueVO keyValueVO2 = new KeyValueVO();
		keyValueVO2.setKey("2");
		keyValueVO2.setValue("默认已付");
		kvList.add(keyValueVO2);
		return kvList;
	}
}
